<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property string $userID
 * @property string $userName
 * @property string $password
 * @property string $contactFirstName
 * @property string $contactLastName
 * @property string $address
 * @property string $city
 * @property string $state
 * @property string $zip
 * @property string $phoneNumber
 * @property string $faxNumber
 * @property string $emailAddress
 * @property string $userType
 * @property string $organizationID
 */
class Users extends CActiveRecord
{
	public $password2;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		
			// Register Rules
			array('user_role_type_ID, user_name, password, password2, contact_first_name, contact_last_name, address1, city, state, zip, phoneNumber, emailAddress, user_type_ID, organization_ID', 'required', 'on'=>'register'),
			array('user_name, contact_first_name, contact_last_name, address1, city, state, zip, phoneNumber, emailAddress', 'required', 'on'=>'update'),
			array('user_name, contact_first_name, contact_last_name, phoneNumber, emailAddress, user_role_type_ID', 'required', 'on'=>'edit'),
			array('user_name, password, contact_first_name, contact_last_name', 'length', 'max'=>20, 'on'=>'register, update, edit'),
			array('password', 'length', 'min'=>6, 'on'=>'register, update, edit'),
			array('user_name', 'unique', 'criteria' => array('select'=>Users::model()->tableName(),
															'condition' => 'binary user_name=:user_name',
															'params'=>array(':user_name'=>$this->user_name)), 'on'=>'register'),
			array('user_name', 'unique', 'criteria' => array('select'=>Users::model()->tableName(),
															'condition' => 'binary user_name=:user_name and user_ID!=:user_ID',
															'params'=>array(':user_name'=>$this->user_name, ':user_ID'=>$this->user_ID)), 'on'=>'update, edit'),															
			array('address1, address2, emailAddress', 'length', 'max'=>50, 'on'=>'register, update, edit'),
			array('emailAddress', 'email', 'fullPattern'=>"$this->emailAddress", 'on'=>'register, update, edit'),
			array('city', 'length', 'max'=>30, 'on'=>'register, update, edit'),
			array('state', 'length', 'max'=>2, 'on'=>'register, update, edit'),
			array('zip', 'length', 'max'=>5, 'on'=>'register, update, edit'),
			array('phoneNumber', 'length', 'max'=>20, 'on'=>'register, update, edit'),//why it's 20 instead of 10?
			array('faxNumber', 'length', 'max'=>10, 'on'=>'register, update, edit'),
			array('phoneNumber', 'match', 'pattern'=>'/^\d{10}|\d{3}-\d{3}-\d{4}$/', 'message'=>'the phone number format is not correct', 'on'=>'register, update, edit'),
			array('faxNumber', 'match', 'pattern'=>'/^\d{10}$/', 'message'=>'the fax number format is not correct', 'on'=>'register, update, edit'),// is fax number length too short?
			array('zip', 'match', 'pattern'=>'/^\d{5}|\d{5}-\d{4}$/', 'message'=>'the zip code format is not correct', 'on'=>'register, update, edit'),
			array('password2', 'compare', 'compareAttribute'=>'password', 'message'=>'two passwords don\'t match', 'on'=>'register, update, edit'),
			
			// Login Rules
			array('user_name, password', 'required', 'on'=>'login'),
		);
	}
	
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'organization' => array(self::BELONGS_TO, 'Organizations', 'organization_ID'),
			'usertype' => array(self::BELONGS_TO, 'UsersTypes', 'user_type_ID'),
			'auctions' => array(self::HAS_MANY, 'Auctions', 'auction_ID'),
			'bidAuctions' => array(self::HAS_MANY, 'BidAuctions', 'user_ID'),
			'orders'=> array(self::HAS_MANY, 'Orders', 'user_ID'),
		);
	}
	
	public function getUserInfo($userID)
	{
		return $this->findByPk($userID);
	}
	
	public function getUser($userID)
	{
		$sql = "SELECT * FROM ".$this->tableName()." WHERE user_ID='".$userID."'";
		$resultset = $this->findAllBySql($sql);
		
		return $resultset;
	}
	
	// returns organization ID of user
	public function getOrganizationID($userID)
	{
		$sql = "SELECT organization_ID FROM ".$this->tableName()." where user_ID='".$userID."'";
		$resultset = $this->findAllBySql($sql);
		
		$organizationID = '';
		
		foreach ($resultset as $record)
			$organizationID = $record->organization_ID;
			
		return $organizationID;
	}
	
	public function getInfoByID($userID)
	{
		return $this->with('organization')->findByPk($userID);
	}
	
	//returns full name of user
	public function getName($userID) {
		$sql = "SELECT contact_first_name, contact_last_name FROM ".$this->tableName()." where user_ID='".$userID."'";
		$resultset = $this->findAllBySql($sql);
			
		return $resultset;
	}
	
	//returns username of user
	public function getUsername($userID) {
		$sql = "SELECT user_name FROM ".$this->tableName()." where user_ID='".$userID."'";
		$resultset = $this->findAllBySql($sql);
			
		foreach ($resultset as $record) {
			$username = $record->user_name;
		}
		return $username;
	}
}